package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import model.DocumentType;

public class DocumentTypeDAOImpl implements DocumentTypeDAO{
	
	 private DataSource dataSource;
	 
	    public void setDataSource(DataSource dataSource) {
	        this.dataSource = dataSource;
	    }
	@Override
	public void save(DocumentType documentType) {
		// TODO Auto-generated method stub
		String query = "insert into documenttype(TypeId, TypeName, Status) value(?,?,?)";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, documentType.getTypeId());
            ps.setString(2, documentType.getTypeName());
            ps.setString(3, documentType.getStatus());
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("documenttype saved with id="+documentType.getTypeId());
            }else System.out.println("documenttype save failed with id="+documentType.getTypeId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	}

	@Override
	public DocumentType getById(int typeId) {
		// TODO Auto-generated method stub
		String query = "select * from documenttype where TypeId =?";
        DocumentType doc = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, typeId);
            rs = ps.executeQuery();
            if(rs.next()){
                doc = new DocumentType();
                doc.setTypeId(typeId);
                doc.setTypeName(rs.getString("TypeName"));
                doc.setStatus(rs.getString("Status"));
                System.out.println(" Found::"+doc);
            }else{
                System.out.println("Nofound with id="+typeId);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		return doc;
	}

	@Override
	public void update(DocumentType documentType) {
		String query = "update documenttype set TypeName=?, Status=? where TypeId=?";
		// TODO Auto-generated method stub
		Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setString(1, documentType.getTypeName());
            ps.setString(2, documentType.getStatus());
            ps.setInt(3, documentType.getTypeId());
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("updated with id="+documentType.getTypeId());
            }else System.out.println("No found with id="+documentType.getTypeId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
	}

	@Override
	public void deleteById(int typeId) {
		// TODO Auto-generated method stub
		String query = "delete from documenttype where TypeId=?";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, typeId);
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println(" deleted with id="+typeId);
            }else System.out.println("No  found with id="+typeId);
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

	}

	@Override
	public List<DocumentType> getAll() {
		// TODO Auto-generated method stub
		String query = "select * from documenttype";
		List<DocumentType> list = new ArrayList<DocumentType>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()){
                DocumentType doc = new DocumentType();
                doc.setTypeId(rs.getInt("TypeId"));
                doc.setTypeName(rs.getString("TypeName"));
                doc.setStatus(rs.getString("Status"));
                list.add(doc);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
		return list;
	}

}
